package com.sky.es.service.impl;

import com.sky.es.entity.ProductDoc;
import com.sky.es.service.ProductBoolQueryEsService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangsk
 */
@Service
@Slf4j
public class ProductBoolQueryEsServiceImpl implements ProductBoolQueryEsService {
    @Autowired
    private ElasticsearchRestTemplate esTemplate;

    @Override
    public List<ProductDoc> getMustByPrice(Long price) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("lv.keyword", "旗舰机"))
                .must(QueryBuilders.termQuery("type.keyword","手机"))
                .filter(QueryBuilders.rangeQuery("price").gte(3999).lte(7999));

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQuery).build();
        SearchHits<ProductDoc> queryResult = esTemplate.search(searchQuery, ProductDoc.class);
        if(null != queryResult && !CollectionUtils.isEmpty(queryResult.getSearchHits())){
            return queryResult.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<ProductDoc> getBollQueryTwo() {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must((new MatchQueryBuilder("name","手机")))
                .must((new TermQueryBuilder("lv.keyword","旗舰机")))
                .filter(new TermQueryBuilder("prict",4999));

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQuery).build();
        SearchHits<ProductDoc> queryResult = esTemplate.search(searchQuery, ProductDoc.class);
        if(null != queryResult && !CollectionUtils.isEmpty(queryResult.getSearchHits())){
            return queryResult.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<ProductDoc> getBollQueryThree() {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.termQuery("type.keyword","电视"))
                .should(QueryBuilders.matchQuery("desc","寸"))
                .filter(QueryBuilders.termQuery("lv.keyword","高端机"));

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQuery).build();
        SearchHits<ProductDoc> queryResult = esTemplate.search(searchQuery, ProductDoc.class);
        if(null != queryResult && !CollectionUtils.isEmpty(queryResult.getSearchHits())){
            return queryResult.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<ProductDoc> getBollQueryFour() {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
                .mustNot(QueryBuilders.matchQuery("name","小米"))
                .should(
                        QueryBuilders.boolQuery()
                                .must(QueryBuilders.matchQuery("name","小米"))
                                .must(QueryBuilders.rangeQuery("price").gte(2000))
                ).should(
                        QueryBuilders.boolQuery()
                                .must(QueryBuilders.termQuery("type.keyword","手机"))
                                .must(QueryBuilders.termsQuery("lv.keyword",new String[]{"旗舰机","高端机"}))
                );

        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQuery).build();
        SearchHits<ProductDoc> queryResult = esTemplate.search(searchQuery, ProductDoc.class);
        if(null != queryResult && !CollectionUtils.isEmpty(queryResult.getSearchHits())){
            return queryResult.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
